<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php
include("global.php");

session_start();

function login()
{
   global $conn;
   
   if(isset ($_POST["user"]) && isset ($_POST["pass"]))
   {
        $u=$_POST["user"];
        $p=md5($_POST["pass"]);
        echo $u.$p;
        
        $sql="SELECT iduser,Username,Email FROM Users WHERE Username='".$u."' and Password='".$p."'";
        $result=@mysql_query($sql,$conn);
        $row=@mysql_fetch_row($result);
        $count=count($row);
        if($count>1)
        {
            $_SESSION['logged']=true;
            $_SESSION['uid']=$row[0];
            $_SESSION['username']=$row[1];
            $_SESSION['group']="";
            echo "<center><font size='5'>Chào bạn <b>".$_SESSION['Username']."</b> đã quay lại !</font></center>";
            header("Location:index.php");
        }
        else
        {
            echo "<center><font size='5'>Username or Password is incorect. Plese try again !</font></center>";
        }
    }
}

function logout()
{
    session_destroy();
    header("Location:index.php");
}

if(isset($_GET["mod"]))
{
    switch($_GET["mod"])
    {
        case "login":
            {
                login();
                break;
            }
        case "logout":
            logout();
            break;        
    }
}
if(isset($_SESSION["logged"]) && $_SESSION['logged'])
    header("Location:index.php");
?>
<h1>Login</h1>
<div id="login_form">
<form id="login" name="login" method="post" action="login.php">
<table>
<tr><td>Username:&nbsp;</td><td><input type="text" name="user" id="user" size="30" /></td></tr>
<tr><td>Password:&nbsp;</td><td><input type="password" name="pass" id="pass" size="30" /></td></tr>
  
<tr><td><input type="submit" name="submit" value="Login" /></td></tr>
</table>
</form>
</div>

</body>
</html>